package com.sai.common.result;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    /**
     * 成功标志
     */
    private boolean success;

    /**
     * 返回处理消息
     */
    private String message;

    /**
     * 返回代码
     */
    private Integer code;

    /**
     * 返回数据对象 data
     */
    private T result;
    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    private Result(ResultCodeEnum e, T result) {
        this.code = e.getCode();
        this.success = this.code == 200;
        this.message = e.getMessage();
        this.result = result;
    }

    private Result(ResultCodeEnum e, String message, T result) {
        this.code = e.getCode();
        this.success = this.code == 200;
        this.message = message;
        this.result = result;
    }

    public final static <T> Result success(T result) {
        return new Result(ResultCodeEnum.SUCCESS, result);
    }

    public final static <T> Result success() {
        return new Result(ResultCodeEnum.SUCCESS, null);
    }

    public static final <T> Result error() {
        return new Result(ResultCodeEnum.ERROR, null);
    }

    public static final <T> Result error(String message) {
        return new Result(ResultCodeEnum.ERROR, message, null);
    }

    public static final <T> Result error(ResultCodeEnum resultCodeEnum) {
        return new Result(resultCodeEnum, null);
    }

}
